import {errorSnackBar, openSnackBar} from '../../util/snack.util';
import {FormGroup} from '@angular/forms';
import {AuthService} from '../../service/auth.service';
import {MatSnackBar} from '@angular/material';
import {ConfigService} from '../../service/config.service';

export class CaptchaComponent {
  formModel: FormGroup;
  imgUrl: string;

  constructor(public snackBar: MatSnackBar,
              public configService: ConfigService, public authService: AuthService) {
    // 如果没有获取配置才获取
    if (!this.configService.globalConfig) {
      this.configService.getConfig().subscribe((config) => {
        this.configService.globalConfig = config;
      }, error1 => {
        errorSnackBar(this.snackBar, error1);
      });
    }
  }

  protected changeCaptcha() {
    this.imgUrl = null;
    this.formModel.get('captcha').setValue('');
    this.authService.getCaptcha().subscribe(captcha => {
      this.imgUrl = captcha.url;
    }, error1 => {
      errorSnackBar(this.snackBar, error1);
    });
  }
}
